package net.starwifi.duobao.service.impl;


import com.axis.sub.EmsSmsUtils;
import net.starwifi.duobao.entity.Member;
import net.starwifi.duobao.entity.SmsLog;
import net.starwifi.duobao.entity.SmsTemplate;
import net.starwifi.duobao.mvc.Message;
import net.starwifi.duobao.service.BaseServiceImpl;
import net.starwifi.duobao.service.MemberService;
import net.starwifi.duobao.service.SmsLogService;
import net.starwifi.duobao.service.SmsTemplateService;
import net.starwifi.duobao.utils.SmsUtils;
import net.starwifi.duobao.utils.StringUtils;
import net.starwifi.duobao.utils.sms.SMSUtils;
import net.starwifi.duobao.utils.times.DateTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Date;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Service
public class SmsLogServiceImpl extends BaseServiceImpl<SmsLog,Long> implements SmsLogService {

    @Autowired
    SmsTemplateService smsTemplateService;
    @Autowired
    MemberService memberService;

    @Transactional
    public Message sendSms(String templateCode,String phone,Map<String,String> params){

       Member member= memberService.findByMobile(phone);
        if(templateCode.equals("register")){
            if(member!=null){
                return Message.error("用户已注册，请直接登录。如忘记密码，请找回密码");
            }
        }
        if(templateCode.equals("wapLogin")){
            if(member!=null&&member.getWxOpenId()!=null){
                return Message.error("该手机号已经绑定过");
            }
        }
        SmsTemplate template=smsTemplateService.findByCode(templateCode);
        if(template==null){
            return Message.error("发送失败,请稍后再试");
        }
        if(!template.isOpen()){
            return Message.error("发送失败,该功能已关闭");
        }
        //单用户日限制
        Date beginData= DateTool.setHms(new Date(), 0, 0, 1);
        Date endData= DateTool.setHms(new Date(), 23, 59, 59);
        Long dayCount=getQueryCount("o.templateId=?1 and o.phone=?2 and o.createDate>?3 and o.createDate<?4",new Serializable[]{template.getId(),phone,beginData,endData});
        if(dayCount>=template.getAllowCount()){
            return Message.error("发送失败,超过每日上限");
        }
        try {
            String content="";
            int telecom=getTelecom(phone);
            if(telecom==4){
                return Message.error("暂时不支持您的号码");
            }else{
                SmsUtils.sendSms(phone, params.get("#code#"),"1674");
            }
//            else if(telecom==3){ //电信
//                 content= SmsUtils.createSMSContent(template.getContent(), params);
               // SmsUtils.sendSms(phone, content);
//            }else{ //联通或者移动
//                if(templateCode.equals("wapLogin")){
//                    content="3.0模板短信";
//                    SMSUtils.send(phone,params.get("{code}"),"10014");
//                }else{
//                     content= SmsUtils.createSMSContent(template.getContent(), params);
//                    EmsSmsUtils.sendMesForEms(telecom, phone, content);
//                }
//            }
            SmsLog smsLog=new SmsLog(template.getId(),template.getName(), content,phone, StringUtils.getUUID(),"success");
            save(smsLog);
        } catch (Exception e) {
            e.printStackTrace();
            return Message.error("短信发送异常");
        }
        return Message.success("发送成功");
    }

    /** 获取运营商（1移动，2联通，3电信，4其他） */
    public static int getTelecom(String mobiles){
        try {
            Pattern p1 = Pattern.compile("^((134|135|136|137|138|139|150|151|152|157|158|159|182|183|184|187|178|188|147))\\d{8}$"); //移动
            Matcher m1 = p1.matcher(mobiles);
            if(m1.matches()){
                return 1;
            }
            Pattern p2 = Pattern.compile("^((130|131|132|145|155|156|176|185|186))\\d{8}$"); //联通
            Matcher m2 = p2.matcher(mobiles);
            if(m2.matches()){
                return 2;
            }
            Pattern p3 = Pattern.compile("^((133|153|180|181|189|177))\\d{8}$"); //电信
            Matcher m3 = p3.matcher(mobiles);
            if(m3.matches()){
                return 3;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return 4;
    }

    public static void main(String[] args) {
        System.out.println(getTelecom("13018070647"));
        System.out.println(getTelecom("18518070647"));
        System.out.println(getTelecom("13418070647"));
        System.out.println(getTelecom("13318070647"));
    }



}
